package com.animee.tally.util;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.animee.tally.adapter.CalendarAdapter;
import com.animee.tally.R;
import com.animee.tally.db.DBManager;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class CalenderDialog extends Dialog implements View.OnClickListener {
    LinearLayout hsvLayout;
    GridView gv;
    ImageView errorIv;

    List<Integer>yearList;  //年份的集合列表
    List<TextView>hsvViewList;
    int selectPos=1;   //被选中的年份的位置
    private CalendarAdapter adapter;
    int selectMonth = -1;
    public CalenderDialog(@NonNull Context context,int selectPos,int selectMonth) {
        super(context);
        this.selectPos = selectPos;
        this.selectMonth = selectMonth;
    }

    public interface OnRefreshListener{
        public void onRefresh(int selectPos,int year,int month);
    }
    OnRefreshListener onRefreshListener;

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_calendar);
        hsvLayout = findViewById(R.id.dialog_calendar_layout);
        gv = findViewById(R.id.dialog_calendar_gv);
        errorIv = findViewById(R.id.dialog_calendar_iv);
        errorIv.setOnClickListener(this);

        addViewToLayout();  //向横向的ScrollView当中添加view的方法
        initGV();
        setGVListener();
    }
//  设置GridVie的点击事件
    private void setGVListener() {
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selPos = position;
                adapter.notifyDataSetChanged();
                int month = position+1;
                int year = adapter.year;
                //取消dialog ，将得到的月份和年份给传回到activity当中
                onRefreshListener.onRefresh(selectPos,year,month);
                cancel();
            }
        });
    }

    private void initGV() {
        int selYear = yearList.get(selectPos);
        adapter = new CalendarAdapter(getContext(), selYear);
        if (selectMonth==-1) {
            Calendar calendar = Calendar.getInstance();
            int month = calendar.get(Calendar.MONTH);
            adapter.selPos=month;
        }else {
            adapter.selPos = selectMonth - 1;
        }
        gv.setAdapter(adapter);
    }

    /* 根据年份向里面添加view*/
    private void addViewToLayout() {
        hsvViewList = new ArrayList<>();    //将添加进线性布局的TextView进行统一管理的集合
        yearList = DBManager.getYearListFromAccount();   //获取数据库当中存储了多少个年份
//        如果数据库当中没有记录，就添加当年的记录信息
        if (yearList.size()==0){
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            yearList.add(year);
        }
        // 便利年份记录，有几年就添加几个view
        for (int i = 0; i < yearList.size(); i++) {
            int year = yearList.get(i);
            View view = getLayoutInflater().inflate(R.layout.item_dialogcal_hsv,null);
            hsvLayout.addView(view);
            TextView hsvTv = view.findViewById(R.id.item_dialogcal_hsv_tv);
            hsvTv.setText(""+year);
            hsvViewList.add(hsvTv);
        }
        if (selectPos==-1) {
            selectPos = hsvViewList.size()-1;
        }
        changeTvbg(selectPos);   //将最后一个设置成被选中状态
        setHSVClickListener();
    }
// 给横向ScrollView当中的每一个TextView添加点击事件的方法
    private void setHSVClickListener() {
        for (int i = 0; i < hsvViewList.size(); i++) {
            TextView view = hsvViewList.get(i);
            final int pos  = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeTvbg(pos);
                    selectPos = pos;
                    //获取被选中的年份，然后让下面的GridView显示的数据源发生变化
                    int selyear = yearList.get(selectPos);
                    adapter.setYear(selyear);
                }
            });
        }
    }

    /* 传入被选中的位置，改变此位置上的背景和文字颜色*/
    public void changeTvbg(int pos){
        for (int i = 0; i < hsvViewList.size(); i++) {
            TextView tv = hsvViewList.get(i);
            tv.setBackgroundResource(R.drawable.dialog_btn_bg);
            tv.setTextColor(Color.BLACK);
        }
        TextView selView = hsvViewList.get(pos);
        selView.setBackgroundResource(R.drawable.main_btn_bg);
        selView.setTextColor(Color.WHITE);
    }

    /* 设置Dialog的尺寸与屏幕尺寸一致*/
    public void setDialogSize(){
        Window window = getWindow();   //获取当前窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();   //获得窗口对象的参数
        Display d = window.getWindowManager().getDefaultDisplay();//获取屏幕宽
        wlp.width = (int) (d.getWidth());//宽度按屏幕大小的百分比设置 ;d.getWidth()这个方法已废弃
        wlp.gravity = Gravity.TOP;
        window.setBackgroundDrawableResource(android.R.color.transparent);  // 设置窗口的背景资源
        window.setAttributes(wlp);

    }

    @Override
    public void onClick(View v) {
        cancel();
    }
}
